$(".like-btn").live('click', function(){
	var element = $(this);
	var current_like = parseInt($(this).find('span.no-like').html());
	if($(this).hasClass('first')) current_like = 0;
	var image_id = $(this).parent().parent().attr('data-id');
	var user_id = $(this).parent().parent().attr('data-user');
	$.ajax({
	    type: "POST",
	    url: base_url+"/execute",
	    data: {
	        execute: 1,
	        user_id: user_id,
	        image_id: image_id
	    },
	    dataType: "json",
	}).done(function(data) {
		if(data.like==0){
			current_like--;
			if(current_like==0){
				element.find('span.no-like').html('1');
				element.addClass('first');
			}
			else element.find('span.no-like').html(current_like);
			element.removeClass('active');
		}else{
			current_like++;
			element.removeClass('first');
			if(!element.hasClass('active')) element.addClass('active');
			element.find('span.no-like').html(current_like);
		}
	});
});

$(".cmtlike-btn").live('click', function(){
	var element = $(this);
	var current_like = parseInt($(this).prev().find('.no-cmtlike').html());
	var comment_id = $(this).attr('data-cmt-id');
	var user_id = $(this).attr('data-user');
	$.ajax({
	    type: "POST",
	    url: base_url+"/execute",
	    data: {
	        execute: 2,
	        user_id: user_id,
	        comment_id: comment_id
	    },
	    dataType: "json",
	}).done(function(data) {
		if(data.like==0){
			current_like--;
			if(current_like==0){
				element.prev().hide();
			}
			else element.prev().find('.no-cmtlike').html(current_like);
			element.removeClass('active');
		}else{
			current_like++;
			if(!element.hasClass('active')) element.addClass('active');
			element.prev().find('.no-cmtlike').html(current_like);
			element.prev().show();
		}
	});
});

$(".delcmt-btn").live('click', function(){
	if(confirm('Bạn có muốn xóa vĩnh viễn nhận xét này không?')){
		var element = $(this);
		var comment_id = $(this).attr('data-cmt-id');
		$.ajax({
		    type: "POST",
		    url: base_url+"/execute",
		    data: {
		        execute: 3,
		        comment_id: comment_id,
		    },
		    dataType: "json",
		}).done(function(data) {
			if(data.success==1){
				element.parent().parent().parent().parent().parent().next().remove();
				element.parent().parent().parent().parent().parent().remove();
			}
		});
	}
});

$(".postedit-btn").live('click', function(){
	if($(this).hasClass('disable')) return;
	$(this).addClass('disable');
	$(this).html('Đang xử lý...');
	var element = $(this);
	var comment_id = $(this).parent().attr('data-cmt-id');
	var comment = $(this).parent().find('.editdiv').html();
	$.ajax({
	    type: "POST",
	    url: base_url+"/execute",
	    data: {
	        execute: 4,
	        comment_id: comment_id,
	        comment: comment,
	    },
	    dataType: "json",
	}).done(function(data) {
		if(data.success==1){
			element.html('Lưu');
			element.removeClass('disable');
			element.prev().find('.editdiv').html(comment);
			element.prev().find('.defaultcmt').html(comment);
			element.parent().prev().find('.Aq .Ct').html(comment);
			element.next().trigger('click');
		}
	});
});

$(".postcmt-btn").live('click', function(){
	if($(this).hasClass('disable')) return;
	$(this).addClass('disable');
	$(this).html('Đang xử lý...');
	var element = $(this);
	var image_id = $(this).attr('data-id');
	var user_id = $(this).attr('data-user');
	var favorite = $(this).attr('data-favorite');
	var comment = $('.cmtdiv').html();
	$.ajax({
	    type: "POST",
	    url: base_url+"/execute",
	    data: {
	        execute: 5,
	        user_id: user_id,
	        image_id: image_id,
	        comment: comment,
	    },
	    dataType: "json",
	}).done(function(data) {
		if(data.success==1){
			popup_gp(image_id, favorite);
		}
	});
});

$('.googleplus .item .view > img').live('click', function(){
	var image_id = $(this).parent().find('.mask').attr('data-id');
	var favorite = $(this).parent().find('.mask').attr('data-favorite');
	$('.super-mask').remove();
	popup_gp(image_id, favorite);
});

$('.googleplus .item .comment-btn').live('click', function(){
	var image_id = $(this).parent().parent().attr('data-id');
	var favorite = $(this).parent().parent().attr('data-favorite');
	$('.super-mask').remove();
	popup_gp(image_id, favorite);
});

function popup_gp(image_id, favorite){
	var mask = '<div class="super-mask">\
					<div class="col-9 image-bar no-left no-right">\
						<div class="image-top">\
							<div class="closegp-btn"></div>\
						</div>\
						<div class="image-mid">\
							<img style="height:75px;width:75px;position:fixed;top: 48%;left: 36%;" src="http://i.imgur.com/6RMhx.gif">\
						</div>\
					</div>\
					<div class="col-3 comment-bar no-left no-right"></div>\
				</div>';
	$('body').append(mask);
	$('body').attr('style','height:100%;position:absolute;overflow:hidden;');
	$('.super-mask').animate({opacity: 1},{
			complete: function () {
				for(var i=0;i<$('.super-mask').length;i++){
					if(i<$('.super-mask').length-1) $('.super-mask')[i].remove(); 
				}
			}
		});
	$('.list-thumb-gp .kNa.G1b').removeClass('active');
	$('#thumbgp'+image_id).addClass('active');
	$.ajax({
	    type: "POST",
	    url: base_url+"/album-plus-detail/"+image_id,
	    dataType: "html",
	    data: {favorite:favorite},
	}).done(function(data) {
		if(data!=''){
			$('.super-mask').html(data);
		}else{
			$('.super-mask').remove();
			$('body').attr('style','');
		}
	});
}

$('.closegp-btn').live('click', function(){
	$('.super-mask').remove();
	$('body').attr('style','');
});

$('.info-toogle').live('click', function(){
	var icon = $(this).find('div');
	if(icon.hasClass('hbtn')){
		icon.removeClass('hbtn');
		icon.addClass('sbtn');
		$('.info-content').hide();
	}else{
		icon.removeClass('sbtn');
		icon.addClass('hbtn');
		$('.info-content').show();
	}
});

$('.prevgp-btn, .nextgp-btn, .image-bar .image-mid .fullgp').live('click',function(){
	var image_id = $(this).attr('data-id');
	var favorite = $(this).attr('data-favorite');
	popup_gp(image_id, favorite);
});

$('.listgp-btn').live('click',function(){
	$('.mask-black').fadeIn();
	$('.list-thumb-gp').css('height','333px');
});

$('.list-thumb-gp .kNa.G1b').live('click',function(){
	var image_id = $(this).attr('data-id');
	var favorite = $(this).attr('data-favorite');
	popup_gp(image_id, favorite);
	$('.closelist-btn').trigger('click');
});

$('.closelist-btn').live('click',function(){
	$('.list-thumb-gp').css('height','0');
	$('.mask-black').fadeOut();
});

$('.mask-black').live('click',function(){
	$('.closelist-btn').trigger('click');
	$('.popup-footer .button-font1').trigger('click');
});

$('.switch-area').live('click',function(){
	if($(this).hasClass('activity')){
		$(this).removeClass('activity');
		$('.like-area').hide();
		$('.comment-area').show();
		$(this).html('Hiển thị hoạt động');
	}else{
		$(this).addClass('activity');
		$('.like-area').show();
		$('.comment-area').hide();
		$(this).html('Hiện thị nhận xét');
	}
});

$('.label-editor').live('click',function(){
	$('.label-editor').hide();
	$('.content-editor').show();
});

$('.backcmt-btn').live('click',function(){
	$('.label-editor').show();
	$('.content-editor').hide();
	$('.content-editor .cmtdiv').html('');
	$('.content-editor .postcmt-btn').removeClass('disable').addClass('disable');
});

$('.backedit-btn').live('click',function(){
	$(this).parent().hide();
	$(this).parent().find('.editdiv').html($(this).parent().find('.defaultcmt').html());
	$(this).parent().find('.postedit-btn').removeClass('disable');
});

$('.editcmt-btn').live('click',function(){
	$('#editcmt'+$(this).attr('data-cmt-id')).show();
});

$('body').on('focus', '[contenteditable]', function() {
    var $this = $(this);
    $this.data('before', $this.html());
    return $this;
}).on('blur keyup paste input', '[contenteditable]', function() {
    var $this = $(this);
    if ($this.data('before') !== $this.html()) {
        $this.data('before', $this.html());
        $this.trigger('change');
        if($(this).hasClass('cmtdiv')){
	        if($(this).html()=='') $('.postcmt-btn').removeClass('disable').addClass('disable');
			else $('.postcmt-btn').removeClass('disable');
		}
		if($(this).hasClass('editdiv')){
	        if($(this).html()=='') $(this).parent().parent().parent().find('.postedit-btn').removeClass('disable').addClass('disable');
			else $(this).parent().parent().parent().find('.postedit-btn').removeClass('disable');
		}
    }
    return $this;
});